'use strict';
const puppeteer = require('puppeteer');
const csv = require('fast-csv');
const fs = require('fs');

(async () => {
    const startUrl = 'https://www.baidu.com/';
    const keyWord = 'helloWord';
    const browser = await puppeteer.launch({
        slowMo: 100, // 放慢速度
        headless: false, // 是否有头
        defaultViewport: { // 界面设置
            width: 1820,
            height: 1080,
        },
        ignoreHTTPSErrors: false, // 忽略 https 报错
        args: ['--start-maximized', '--no-sandbox', '--disable-setuid-sandbox'],
    });
    const page = await browser.newPage();
    await page.goto(startUrl).catch(error => console.log(error));
    // await page.waitFor(1 * 1000);
    await page.click('#head_wrapper .s_ipt_wr');
    await page.type('.s_ipt', keyWord, {
        delay: 100
    });
    //官方文档上说当点击的按钮存在跳转的时候要加一个等待函数。
    await Promise.all(
        [page.waitForNavigation({
                'timeout': 10000,
                'waitUntil': 'networkidle0'
            }, ),
            page.click('.wrapper_new .s_btn_wr')
        ]
    ).catch(err => {
        console.error(err)
    });

    console.log(page,'page')
    let content = await page.$eval('#container .new_nums .nums_text', content => content.innerText);
    let dataCount = parseInt(content.replace(/[^0-9]/ig, ""));

    console.log(dataCount, 'dataCount');
    if (dataCount && dataCount > 0) {
        const dataEle = await page.$$('.result.c-container.new-pmd');
        console.log(dataEle.length);
        const stream = fs.createWriteStream('puppeteer.csv');
        const csvStream = csv.format({
            headers: true
        });
        csvStream.pipe(stream).on('end', process.exit);
        for (let index = 0; index < dataEle.length; index++) {
            const element = dataEle[index];
            const title = await element.$eval('a', el => el && el.innerHTML).catch(error => console.error(error))
            const desc = await element.$eval('.c-abstract', el => el && el.innerHTML).catch(error => console.error(error))
            csvStream.write({
                标题: title || '',
                摘要: desc || '',
            });
        }
        csvStream.end(() => {
            console.log('写入完毕');
        });
    }
    await page.screenshot({
        path: 'puppeteer.png'
    });
    await browser.close();
})();